package shuogg.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import shuogg.domain.User;
import shuogg.domain.UserCustom;

/**
Test:
    http://localhost:8080/springMVC_testAnno/user/receiveUsername.do?username=shuoGG&userage=12&usergender=%E7%94%B7
    http://localhost:8080/springMVC_testAnno/user/receiveUserage.do?username=shuoGG&userage=12&usergender=%E7%94%B7
    http://localhost:8080/springMVC_testAnno/user/receiveUsergender.do?username=shuoGG&userage=12&usergender=%E7%94%B7
    http://localhost:8080/springMVC_testAnno/user/receiveUserFull.do?username=shuoGG&userage=12&usergender=%E7%94%B7
 */

// "/user" 也可以写成 "user", 即"/"可以省略
@Controller
@RequestMapping("/user")
public class UserController {
    /* 方法名和注解value可以随便写, 但是形参必须是Parameter的key */
    @RequestMapping("/receiveUsername")
    public String receiveUsername(String username) {
        System.out.println(username);
        return "index";
    }

    @RequestMapping("/receiveUserage")
    public String receiveUserage(String userage) {
        System.out.println(userage);
        return "index";
    }

    @RequestMapping("/receiveUsergender")
    public String receiveUsergender(String usergender) {
        System.out.println(usergender);
        return "index";
    }

    @RequestMapping("/receiveUserFull")
    public String receiveUserFull(User user, Model model) {
        System.out.println(user);
        model.addAttribute("myuserData", user);
        return "showResult";
    }

    @RequestMapping(value = "/receiveUserCustom", method = { RequestMethod.GET, RequestMethod.POST })
    public String receiveUserCustom(UserCustom userCustom) {
        System.out.println(userCustom);
        return "showResultTable";
    }
    
    @RequestMapping("/forwardTest")
    public String forwardTest() {
        return "forward:sendUserList.do";
    }
    
    @RequestMapping("/redirectTest")
    public String redirectTest() {
        return "forward:sendUserList.do";
    }
    
    @RequestMapping("/includeTest")
    public String forwardToOtherTest() {
        return "forward:/mycontroller/simpleshow.do";
    }
    
    @RequestMapping("/update/{username}")
    public String updateUserNamePath(@PathVariable String username, Model model) {
        User user = new User();
        user.setUsername(username);
        user.setUserage("10");
        user.setUsergender("男");
        model.addAttribute("myuserData", user);
        return "showResult";
    }     
    
    @RequestMapping("/updateNameGet1")
    public String updateUserNameGet1(@RequestParam("username") String name, Model model) {
        User user = new User();
        user.setUsername(name);
        user.setUserage("100");
        user.setUsergender("女");
        model.addAttribute("myuserData", user);
        return "showResult";
    }  
    
    @RequestMapping("/updateNameGet2")
    public String updateUserNameGet2(String username, Model model) {
        User user = new User();
        user.setUsername(username);
        user.setUserage("100");
        user.setUsergender("女");
        model.addAttribute("myuserData", user);
        return "showResult";
    }  
    
    @RequestMapping("sendUserList")
    public String sendUserList(Model model) {
        User user1 = new User();
        user1.setUsername("Xiaohong");
        user1.setUserage("12");
        user1.setUsergender("男");
        User user2 = new User();
        user2.setUsername("Xiaoming");
        user2.setUserage("92");
        user2.setUsergender("男");
        
        User user3 = new User();
        user3.setUsername("Lyn");
        user3.setUserage("22");
        user3.setUsergender("男");
        
        List<User> userlist = new ArrayList<>();
        userlist.add(user1);
        userlist.add(user2);
        userlist.add(user3);
        
        
        model.addAttribute("userList", userlist);
        return "showResultTable";
    }
}
